﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MainUIController : MonoBehaviour {

    private int i_tempMainUI = 1;        //主页选择框的索引值
    public Transform tf_AllUI;           //所有界面的父集

    // Use this for initialization
    void Start () {
        EventManager<bool>.AddEvent(MyEventType.UpButtonClickEvent, OnUpButtonClick_MainUI);
        EventManager<bool>.AddEvent(MyEventType.DownButtonClickEvent, OnDownButtonClick_MainUI);
        EventManager<bool>.AddEvent(MyEventType.LeftButtonClickEvent, OnLeftButtonClick_MainUI);
        EventManager<bool>.AddEvent(MyEventType.RightButtonClickEvent, OnRightButtonClick_MainUI);
        EventManager<bool>.AddEvent(MyEventType.SetButtonClickEvent, OnSetButtonClick_MainUI);
        EventManager<bool>.AddEvent(MyEventType.ChongzhiShujuEvent,Reset);
	}

    private void Reset(bool obj)
    {
        if (tf_AllUI.GetChild(tf_AllUI.childCount - 1).name == "mainUI") {
            UIController._instance.ChangeGaoliang(transform, i_tempMainUI, false);
            i_tempMainUI = 1;
            UIController._instance.ChangeGaoliang(transform, i_tempMainUI, true);
        }    
    }

    void OnUpButtonClick_MainUI(bool value) {
        if (tf_AllUI.GetChild(tf_AllUI.childCount - 1).name == "mainUI")
        {
            if (i_tempMainUI > 2 && i_tempMainUI <= transform.childCount - 1)
            {
                UIController._instance.ChangeGaoliang(transform, i_tempMainUI, false);
                i_tempMainUI -= 2;
                UIController._instance.ChangeGaoliang(transform, i_tempMainUI, true);
            }
        }
    }

    void OnDownButtonClick_MainUI(bool value) {
        if (tf_AllUI.GetChild(tf_AllUI.childCount - 1).name == "mainUI")
        {
            if (i_tempMainUI >= 1 && i_tempMainUI < transform.childCount - 3)
            {
                UIController._instance.ChangeGaoliang(transform, i_tempMainUI, false);
                i_tempMainUI += 2;
                UIController._instance.ChangeGaoliang(transform, i_tempMainUI, true);
            }
        }
    }

    void OnLeftButtonClick_MainUI(bool value) {
        if (tf_AllUI.GetChild(tf_AllUI.childCount - 1).name == "mainUI")
        {
            if (i_tempMainUI > 1 && i_tempMainUI <= transform.childCount - 1)
            {
                if (i_tempMainUI % 2 == 0)
                {
                    UIController._instance.ChangeGaoliang(transform, i_tempMainUI, false);
                    i_tempMainUI -= 1;
                    UIController._instance.ChangeGaoliang(transform, i_tempMainUI, true);
                }
            }
        }
    }

    void OnRightButtonClick_MainUI(bool value) {
        if (tf_AllUI.GetChild(tf_AllUI.childCount - 1).name == "mainUI")
        {
            if (i_tempMainUI >= 1 && i_tempMainUI < transform.childCount - 1)
            {
                if (i_tempMainUI % 2 != 0)
                {
                    UIController._instance.ChangeGaoliang(transform,i_tempMainUI,false);
                    i_tempMainUI += 1;
                    UIController._instance.ChangeGaoliang(transform, i_tempMainUI, true);
                }
            }
        }
    }

    void OnSetButtonClick_MainUI(bool value) {
        //运行设置
        if (tf_AllUI.GetChild(tf_AllUI.childCount - 1).name == "mainUI") {
            if (i_tempMainUI == 3)
            {
                for (int i = 0; i < tf_AllUI.childCount; i++)
                {
                    if (tf_AllUI.GetChild(i).name == "yunxingshezhiGO")
                    {
                        
                        TimerMngr.Instance.Create(() =>
                        {
                            EventManager<bool>.TriggerEvent(MyEventType.ChongzhiShujuEvent, true);
                            tf_AllUI.GetChild(i).SetAsLastSibling();
                        }).Start(0.1f);
                        break;
                    }
                }
            }
            //定值操作
            else if (i_tempMainUI == 5)
            {
                for (int i = 0; i < tf_AllUI.childCount; i++)
                {
                    if (tf_AllUI.GetChild(i).name == "dingzhicaozuoGO")
                    {
                        TimerMngr.Instance.Create(() => {
                            EventManager<bool>.TriggerEvent(MyEventType.ChongzhiShujuEvent, true);
                            tf_AllUI.GetChild(i).SetAsLastSibling();
                        }).Start(0.1f);
                        
                        break;
                    }
                }
            }
        }
    }
}
